from django.db import models
from users.models import *
import os


# 品牌
class Brand(models.Model):
    name = models.CharField('品牌名称',max_length=20)
    first_name = models.CharField('品牌首字母',max_length=10,default=None)
    logo = models.CharField('品牌logo',max_length=200)
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'brand'
        verbose_name_plural = '品牌表'

# 商品种类表
class Cate(models.Model):
    name = models.CharField("商品类别", max_length=30)
    cid = models.ForeignKey('self', on_delete=models.CASCADE,verbose_name='父级类别',related_name='subs',null=True,blank=True)
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'cate'
        verbose_name_plural = '商品类别'

# 频道组
class Group(models.Model):
    name = models.CharField('频道组名',max_length=30)
    cate = models.OneToOneField(Cate,on_delete=models.CASCADE,verbose_name='一级类别')
    group_url = models.CharField('频道组链接',max_length=100,default='http://jd.com')
    sequence = models.IntegerField('展示顺序',default=0)
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'group'
        verbose_name_plural = '频道组'

# SPU 标准的产品单位
class SPU(models.Model):
    name = models.CharField('spu',max_length=20)
    # PROTECT 保护 主表的数据删除 如果子表有数据引用则不删除
    brand = models.ForeignKey(Brand,on_delete=models.PROTECT,verbose_name='品牌')
    category1 = models.ForeignKey(Cate,on_delete=models.CASCADE,verbose_name='一级类别',
                                 # 反向查询字段
                                 related_name='cate1_spu')
    category2 = models.ForeignKey(Cate,on_delete=models.CASCADE,verbose_name='二级类别',related_name='cate2_spu')
    category3 = models.ForeignKey(Cate,on_delete=models.CASCADE,verbose_name='三级类别',related_name='cate3_spu')
    comment_num = models.IntegerField('评论量',default=0)
    sales = models.IntegerField('销量',default=0)
    desc_detail = models.TextField('详情描述',default='')
    desc_pack = models.TextField('包装信息',default='')
    desc_service = models.TextField('售后服务',default='')
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'spu'
        verbose_name_plural = '商品spu'

# 商品表SKU 标准的库存单位
class SKU(models.Model):
    name = models.CharField('商品名称',max_length=100)
    describe = models.CharField('描述信息',max_length=500,default='')
    price = models.DecimalField('商品价格',max_digits=8,decimal_places=2)
    stock = models.IntegerField('商品库存',default=0)
    sales = models.IntegerField('销量',default=0)
    detail = models.TextField('商品详情',default='')
    image_default = models.CharField('默认图片',max_length=300)
    is_selling = models.BooleanField('是否在售',default=False)
    cate = models.ForeignKey(Cate,on_delete=models.CASCADE,verbose_name='商品类别')
    spu = models.ForeignKey(SPU,on_delete=models.CASCADE,verbose_name='商品的SPU')
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'sku'
        verbose_name_plural = '商品sku'

# SPU规格管理
class SPUSpecification(models.Model):
    name = models.CharField('规格名称',max_length=20)
    spu = models.ForeignKey(SPU,on_delete=models.CASCADE,verbose_name='商品SPU')
    def __str__(self):
        return '%s: %s' % (self.spu.name, self.name)
    class Meta:
        db_table = 'spu_specification'
        verbose_name_plural = 'SPU规格'

# 规格选项
class SpecValue(models.Model):
    value = models.CharField('选项值',max_length=20)
    spec = models.ForeignKey(SPUSpecification,on_delete=models.PROTECT,verbose_name='规格',related_name='spec_value')
    class Meta:
        db_table = 'spec_value'
        verbose_name_plural = '规格选项值'
    def __str__(self):
        return '%s-%s'%(self.spec,self.value)

# sku的规格
class SKUSpec(models.Model):
    sku = models.ForeignKey(SKU,on_delete=models.CASCADE,verbose_name='sku')
    spec = models.ForeignKey(SPUSpecification,on_delete=models.PROTECT,verbose_name='规格名称')
    value = models.ForeignKey(SpecValue,on_delete=models.PROTECT,verbose_name='规格值')
    class Meta:
        db_table = 'sku_spec'
        verbose_name_plural = 'sku规格'
    def __str__(self):
        return '%s:%s-%s'%(self.sku.name,self.spec.name,self.value.value)

# 商品sku图片
class SKUImg(models.Model):
    sku = models.ForeignKey(SKU,on_delete=models.CASCADE,verbose_name='商品sku')
    image = models.CharField('商品图片',max_length=300)
    class Meta:
        db_table = 'sku_image'
        verbose_name_plural = 'sku图片'
    def __str__(self):
        return '%s %s'%(self.sku.name,os.path.basename(self.image))

















